package br.elsa.survey.view.beans.managed.tests;

import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.*;

import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import br.elsa.survey.dao.exceptions.DataNotFoundException;
import br.elsa.survey.domain.questions.Question;
import br.elsa.survey.domain.survey.Survey;
import br.elsa.survey.services.QuestionService;
import br.elsa.survey.services.SurveyService;
import br.elsa.survey.domain.survey.SurveyFactory;
import br.elsa.survey.view.beans.managed.SurveyCreationView;
import br.elsa.survey.view.messages.ViewMessages;

public class SurveyCreationViewTest {

	private SurveyCreationView surveyCreationView;
	
	@Mock
	private QuestionService questionService;
	
	@Mock
	private SurveyService surveyService;
	
	@Mock
	private ViewMessages viewMessages;
	
	@Mock
	private SurveyFactory surveyFactory;
	
	@Mock
	private List<Question> questions;
	
	@Mock
	private Survey newSurvey;
	
	@Before
	public void setUp() throws Exception {
		MockitoAnnotations.initMocks(this);
		this.surveyCreationView = new SurveyCreationView(surveyService, questionService, viewMessages, questions, newSurvey, surveyFactory);
	}

	@Test
	public void loadQuestionsShouldCallQuestionServiceForAllQuestions() throws DataNotFoundException {
		
		surveyCreationView.loadQuestions();
		verify(questionService, times(1)).getAllQuestions();
	}
	
	@Test
	public void loadQuestionsShouldAddWarnMessageToViewIfThereIsNoQuestionRegisteredInTheSystem() throws DataNotFoundException {
		
		when(questionService.getAllQuestions()).thenThrow(new DataNotFoundException());
		
		surveyCreationView.loadQuestions();
		
		verify(viewMessages, times(1)).addWarnMessage(anyString());
	}

	@Test
	public void selectQuestionShouldAddQuestionToSelectedQuestionsListAndRemoveTheQuestionFromQuestionsList() {
		
		Question question = mock(Question.class);
		surveyCreationView.selectQuestion(question);
		
		verify(newSurvey, times(1)).addQuestion(question);
		verify(questions, times(1)).remove(question);
	}

	@Test
	public void removeQuestionShouldRemoveQuestionFromSelectedQuestionsListAndAddItToQuestionsList() {
		
		Question question = mock(Question.class);
		surveyCreationView.removeQuestion(question);
		
		verify(newSurvey, times(1)).removeQuestion(question);
		verify(questions, times(1)).add(question);
	}

	@Test
	public void resetViewStateShouldCreateNewEmptySurveyAndLoadAllQuestions() {
		
		SurveyCreationView spy = spy(surveyCreationView);
		
		spy.resetViewState();
		
		verify(surveyFactory, times(1)).createSurvey();
		verify(spy, times(1)).resetViewState();
	}
	
	@Test
	public void createSurveyShouldCallSurveyServiceAddMessageToViewAndResetViewState() {
		
		SurveyCreationView spy = spy(surveyCreationView);
		
		spy.createSurvey();
		
		verify(surveyService, times(1)).create(newSurvey);
		verify(viewMessages, times(1)).addInfoMessage(anyString());
		verify(spy, times(1)).resetViewState();
	}
}
